<?php
class Validator
{
	public function __construct()
	{
		
	}
	public function ValidateGET()
	{
		if(isset($_GET))
		{
			$get = $_GET; 
			foreach($get as $key=>$value)
			{
				$newValue = strip_tags($value);
				$newValue = mysql_escape_string($newValue);
				$newValue = htmlspecialchars($newValue);
				$_GET[$key] = $newValue;
			}

		}
		
	}
	public function ValidatePOST()
	{
		if(isset($_POST))
		{
			$post = $_POST; 
			foreach($post as $key=>$value)
			{
				$newValue = strip_tags($value);
				$newValue = mysql_escape_string($newValue);
				$newValue = htmlspecialchars($newValue);
				$_POST[$key] = $newValue;
			}

		}
		
	}
	public function ValidateEmail($email)
	{
		
		if(preg_match('/^[a-zA-Z0-9\.\-\_]+\@[a-zA-Z0-9\.\-\_]+\.[a-z]{2,4}$/D', $email))

		{
			return true;
		}
		else
		{
			return false; 
		}
			
	}
	public function ValidatePostCode($post)
	{
		
			if(preg_match('/^[0-9]{2}-[0-9]{3}$/D', $post))
			{
				return true;
			}
			else
			{
				return false;
			}
			 
	}
	public function ValidatePhone($phone) 
	{
		if(preg_match('/^[0-9\-\(\)]{8,25}$/D',$phone))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	public function ValidateWWW($www)
	{
		if(preg_match('/^((http\:\/\/){1})?(([w]{3}\.){1})?[a-zA-Z\_\-\.]+\.([a-z]{2,4}){1,2}$/D',$www))  
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	public function ValidateSafeChars($text)
	{
		if(preg_match('/^[a-zA-ZąęćśłóńźżĄĘĆŚŁÓŃŻŹ]{1,}$/D', $text))
		{
			return true;
		} 
		else
		{
			return false;
		}
	}
	public function ValidateSafeCharsAndNumbers($text)
	{
		if(preg_match('/^[a-zA-ZąęćśłóńźżĄĘĆŚŁÓŃŻŹ1-9\ ]{1,}$/D', $text))
		{
			return true;
		} 
		else
		{
			return false;
		}
	}
	public function ValidateLogin($text)
	{
		if(preg_match('/^[a-zA-ZąęćśłóńźżĄĘĆŚŁÓŃŻŹ0-9\.\_\@\!\$\%\ ]{5,}$/D', $text))
		{
			return true;
		} 
		else
		{
			return false;
		}
	}
	public function ValidatePass($text)
	{
		if(preg_match('/^[a-zA-ZąęćśłóńźżĄĘĆŚŁÓŃŻŹ\.\_\@\!\$\%\ ]{5,}[1-9]{2,}$/D', $text))
		{
			return true;
		} 
		else
		{
			return false;
		}
	}
	
	/**
	 * Funkcja sprawdza poprawność daty podanej jako parametr
	 *
	 * @param String $date - data w formacie YYYY-MM-DD
	 * @return boolean
	 */
	public function ValidateDate($date)
	{
		$data = explode('-',$date);
		if (empty($data[1]) || empty($data[2]) || empty($data[0]))
			return false;
		else
		{
			if(checkdate($data[1],$data[2],$data[0]))
			{
				
				return true;
				
			}
			else 
			{
				
				return false;
				
			}
		}
		
	}
	
	/** 
	 * Validaowanie numeru nip
	 * @param object $nip
	 * @param object $required [optional]
	 * @return 
	 */
	public function ValidateNip($nip, $required = true)
	{
		if ($required === false && $nip == '') {
			return true;
		}
		elseif ($required === true && $nip == '') {
			return 'Pole jest wymagane';
		}
		if (preg_match('/^[0-9]{3}-[0-9]{3}-[0-9]{2}-[0-9]{2}$/D', $nip)) {
			return true;
		}
		else {
			return 'Nip ma nieprawidłowy format. Prawidłowy format to 999-999-99-99';
		}
			 
	}
	
	public function ValidateIfImage($fileName)
	{
		$type = $_FILES[$fileName]['type'];
		$type = explode('/',$type);
		
		if($type[0] == 'image')
		{
			return true;
		}
		else
		{
			return false;
		}
	}
		
	public function ValidateImageFormats($aviableFormats, $fileName)
	{
		$validate = false;
		
	 	foreach($aviableFormats as $key=>$value)
		{
			$fileFormat = explode("/", $_FILES[$fileName]['type']);
			if($fileFormat[1] == $value)
			{
				$validate = true;
			}
		}
		
		return $validate;
	}
	
	public function ValidateImageSize($maxSize, $fileName)
	{
		$validate = true;
		
		if($_FILES[$fileName]['size']>$maxSize)
		{
			$validate = false;
		}
		
		return $validate;
	}

        public function ValidateEmpty($str)
        {
            if(isset($str) && $str != '')
            {
                return true;
            }
            else
            {
                return false;
            }
        }
}
?>